SNA Descritive Analysis from “Projeto Redes de Atenção às pessoas que consomem álcool e outras Drogas em Juiz de Fora-MG Brazil” - SNArRDJF

Here you can find a basic script to analysis data from SNArRDJF - this script was elaborated considering its use for orther matrix adjacency data from SNArRDJF - Here we are going to analyse:

1 28_ACESSO

########################## Basic Preparation ##### `#########################

2 Loading objects generated with previous script

rm(list = ls()) # removing previous objects to be sure that we don't have objects conflicts name
load("~/SNArRDJF/Robject/acesso_data.RData")

2.1 Reload packages

suppressMessages(library(RColorBrewer))
#suppressMessages(library(car))
#suppressMessages(library(xtable))
suppressMessages(library(igraph))
#suppressMessages(library(miniCRAN))
#suppressMessages(library(magrittr))
#suppressMessages(library(keyplayer))
#suppressMessages(library(dplyr))
#suppressMessages(library(feather))
#suppressMessages(library(visNetwork))
#suppressMessages(library(knitr))
suppressMessages(library(DT))

2.2 Adding phantom tools

#In order to get dinamic javascript object install those ones. If you get problems installing go to Stackoverflow.com and type your error to discover what to do. In some cases the libraries need to be intalled in outside R libs.
#devtools::install_github("wch/webshot")
#webshot::install_phantomjs()

2.3 Setting a random seed - this is a good strategy to keep the same graph pattern layout in a new report generation

set.seed(123)

2.4 Simplify Graph - removing loops and duble edges

#acesso<-simplify(acesso) #Simplify

3 Centrality Measures

• For undirected graphs:

– Actor centrality - involvement (connections) with other actors

• For directed graphs:

– Actor centrality - source of the ties (outgoing edges)

– Actor prestige - recipient of many ties (incoming edges)

In general - high centrality degree means direct contact with many other actors

3.1 Centrality Degree (number of ties/nearest neighbors).

3.2 Saving in igrpah object

V(acesso)$indegree<-degree(acesso, mode = "in") # Actor prestige - recipient of many ties (incoming edges)
V(acesso)$outdegree <- degree(acesso, mode = "out") # Actor centrality - source of the ties (outgoing edges)
V(acesso)$totaldegree <- degree(acesso, mode = "total")

3.3 Saving in Global Environment as an object

acesso_indegree<-degree(acesso, mode = "in")
acesso_outdegree<-degree(acesso, mode = "out")
acesso_totaldegree<-degree(acesso, mode = "total")

4 Centrality Degree Descriptive Statistics - non-normalized

4.1 Centrality Degree Descriptive Statistics - In

##in
summary(acesso_indegree)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   3.500   5.000   8.364   9.000 106.000
sd(acesso_indegree)
## [1] 10.83237

4.2 Histogram acesso degree - In

hist(degree(acesso, mode = "in", normalized = F), ylab="Frequency", xlab="Degree",  breaks=vcount(acesso)/10, main="Histogram of Indegree Nodes - 28_ACESSO")

4.3 Centrality Degree Descriptive Statistics - Out

##out
summary(acesso_outdegree)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   1.000   4.000   8.364   8.000 105.000
sd(acesso_outdegree)
## [1] 13.97334

4.4 Histogram acesso degree - Out

hist(degree(acesso, mode = "out", normalized = F), ylab="Frequency", xlab="Degree",  breaks=vcount(acesso)/10, main="Histogram of Outdegree Nodes - 28_ACESSO")

4.5 Centrality Degree Descriptive Statistics - All

##all
summary(acesso_totaldegree)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    5.00   10.00   16.73   18.00  211.00
sd(acesso_totaldegree)
## [1] 23.44349

4.6 Histogram acesso degree - All

hist(degree(acesso, mode = "all", normalized = F), ylab="Frequency", xlab="Degree",  breaks=vcount(acesso)/10, main="Histogram of All Degree Nodes - 28_ACESSO")

5 Compute strength - weighted

A slightly more nuanced metric is “strength centrality”, which is defined as the sum of the weights of all the connections for a given node. This is also sometimes called “weighted degree centrality”

V(acesso)$acesso_strength<- strength(acesso, weights=E(acesso)$weight)
acesso_strength<- strength(acesso, weights=E(acesso)$weight)

5.1 Strength Stats

summary(acesso_strength)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   -4.00   24.00   38.00   80.11   83.50 1111.00
sd(acesso_strength)
## [1] 117.4326

5.2 Histogram acesso degree - All

hist(strength(acesso, weights=E(acesso)$weight), ylab="Frequency", xlab="Degree",  breaks=vcount(acesso)/10, main="Histogram of Strength Degree Nodes - 28_ACESSO")

6 Centrality Degree Normalized

6.1 Centrality Degree Normalized saving igraph object

V(acesso)$indegree_n<-degree(acesso, mode = "in", normalized = T)
V(acesso)$outdegree_n<- degree(acesso, mode = "out", normalized = T)
V(acesso)$totaldegree_n<- degree(acesso, mode = "total", normalized = T)

6.2 Saving in Global Environment as an object

acesso_indegree_n<-degree(acesso, mode = "in", normalized = T)
acesso_outdegree_n<-degree(acesso, mode = "out", normalized = T)
acesso_totaldegree_n<-degree(acesso, mode = "total", normalized = T)

6.3 Centrality Degree Normalized Descriptive Statistics - in

summary(acesso_indegree_n)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## 0.00000 0.01882 0.02688 0.04497 0.04839 0.56990
sd(acesso_indegree_n)
## [1] 0.05823853

6.4 Histogram acesso degree normalized - in

hist(degree(acesso, mode = "in", normalized = T), ylab="Frequency", xlab="Normalized Degree",  breaks=vcount(acesso)/10, main="Histogram of Normalized Indegree Nodes - 28_ACESSO")

6.5 Centrality Degree Normalized Descriptive Statistics - out

summary(acesso_outdegree_n)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.000000 0.005376 0.021510 0.044970 0.043010 0.564500
sd(acesso_outdegree_n)
## [1] 0.07512547

6.6 Histogram acesso degree normalized - out

hist(degree(acesso, mode = "out", normalized = T), ylab="Frequency", xlab="Normalized Degree",  breaks=vcount(acesso)/10, main="Histogram of Normalized Outdegree Nodes - 28_ACESSO")

6.7 Centrality Degree Normalized Descriptive Statistics - all

summary(acesso_totaldegree_n)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.005376 0.026880 0.053760 0.089930 0.096770 1.134000
sd(acesso_totaldegree_n)
## [1] 0.1260403

6.8 Histogram acesso degree normalized - all

hist(degree(acesso, mode = "all", normalized = T), ylab="Frequency", xlab="Normalized Degree",  breaks=vcount(acesso)/10, main="Histogram of Normalized All Degree Nodes - 28_ACESSO")

7 Centralization Degree

V(acesso)$acesso_centr_degree <- centralization.degree(acesso)$res
acesso_centr_degree <- centralization.degree(acesso)

7.1 Centralization

acesso_centr_degree$centralization
## [1] 0.5250462

7.2 Theoretical Max

acesso_centr_degree$theoretical_max
## [1] 69192

8 Degree distribution considering total equal one

acesso_degree.distribution<-degree.distribution(acesso)

8.1 Degree distribution Descriptive Stats

summary(acesso_degree.distribution)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## 0.000000 0.000000 0.000000 0.004717 0.000000 0.096260
sd(acesso_degree.distribution)
## [1] 0.01452209

8.2 Histogram acesso distribution degree

hist(degree.distribution(acesso), breaks=vcount(acesso)/10, ylab="Frequency", xlab="Degree Distribuition", main="Histogram of Degree Distribuition - 28_ACESSO")

8.3 Degree Probability Distribution

dd <- degree.distribution(acesso, cumulative=T, mode="all")

8.4 Degree Probability Distribution - Plot Cumulative Frequency

plot(dd, pch=19, cex=1, col="orange", xlab="Degree", ylab="Cumulative Frequency", main= "Cumulative Frequency of 28_ACESSO ")

9 Log-Log Degree Distribution - Scale Free Network - Does it fit to power law ?

dd.acesso <- degree.distribution(acesso)
d <- 1:max(degree(acesso))-1
ind <- (dd.acesso != 0)

9.1 Plot Log-Log Degree Distribution

plot(d[ind], 
     dd.acesso[ind], 
     log="xy", 
     col="blue",
     xlab=c("Log-Degree"), 
     ylab=c("Log-Intensity"),
     main="Log-Log Degree Distribution For 28_ACESSO"
     )

10 Average Neighbor Degree versus Vertex Degree (log-log scale for 28_ACESSO)

The neighborhood of a given order y of a vertex v includes all vertices which are closer to v than the order. Ie. order y=0 is always v itself, order 1 is v plus its immediate neighbors, order 2 is order 1 plus the immediate neighbors of the vertices in order 1, etc.

10.1 Simplify graph first

acesso_simplified<-simplify(acesso)

10.2 Average Neighbor Degree versus vertex degree (log-log scale for acesso)

acesso_a.nn.deg <- graph.knn(acesso_simplified, weights =E(acesso_simplified)$weight)$knn %>% round(1)

10.3 Saving to igraph object

V(acesso_simplified)$acesso_a.nn.deg <- graph.knn(acesso_simplified, weights=E(acesso_simplified)$weight)$knn

10.4 Table Average Neighbor Degree

d<-cbind(V(acesso_simplified)$LABEL_COR,acesso_a.nn.deg)
datatable(d)

10.5 Plotting Average Neighbor Degree versus vertex degree

plot(degree(acesso_simplified), 
     acesso_a.nn.deg, 
     log="xy", 
     col="goldenrod", 
     xlab=c("Log Vertex Degree"),
     ylab=c("Log Average Neighbor Degree"),
     main="Average Neighbor Degree vs Vertex Degree - Log-Log Scale for 28_ACESSO"
     )

11 Average Weighted Neighbor Degree versus vertex degree (log-log scale for weighted 28_ACESSO)

acesso_a.nn.deg_w <- graph.knn(acesso_simplified, weights=E(acesso_simplified)$weight)$knn %>% round(1)

11.1 Saving to igraph object

V(acesso_simplified)$acesso_a.nn.deg_w <-acesso_a.nn.deg <- graph.knn(acesso_simplified, weights=E(acesso_simplified)$weight)$knn

11.2 Average Weighted Neighbor Descriptive

summary(acesso_a.nn.deg_w)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
##   -1.80   32.47   49.95   89.63  116.60  906.80       1
sd(acesso_a.nn.deg_w, na.rm = T)
## [1] 115.3789

11.3 Table Average Neighbor Degree Weighted

d<-cbind(V(acesso_simplified)$LABEL_COR,acesso_a.nn.deg_w)
datatable(d)

11.4 Plotting Average Neighbor Degree versus vertex degree

plot(degree(acesso_simplified), 
     acesso_a.nn.deg, 
     log="xy", 
     col="goldenrod", 
     xlab=c("Log Vertex Degree"),
     ylab=c("Log Average Neighbor Degree"),
     main="Average Weighted Neighbor Degree vs Vertex Degree - Log-Log Scale For Weighted 28_ACESSO"
     )

12 Degree Centralities Dinamic Table

12.1 Getting Degree Measures

acesso_indegree<-degree(acesso, mode = "in")
acesso_outdegree<-degree(acesso, mode = "out")
acesso_totaldegree<-degree(acesso, mode = "total")
acesso_strength<- strength(acesso, weights=E(acesso)$weight)
acesso_indegree_n<-degree(acesso, mode = "in", normalized = T) %>% round(3)
acesso_outdegree_n<-degree(acesso, mode = "out", normalized = T) %>% round(3)
acesso_totaldegree_n<-degree(acesso, mode = "total", normalized = T) %>% round(3)
acesso_centr_degree <- centralization.degree(acesso)$res
acesso_a.nn.deg <- graph.knn(acesso_simplified)$knn %>% round(1)
acesso_a.nn.deg_w <- graph.knn(acesso_simplified, weights=E(acesso_simplified)$weight)$knn %>% round(1)

12.2 Creating a dataframe of measures

acesso_df_degree <- data.frame(acesso_indegree,
acesso_outdegree, 
acesso_totaldegree,
acesso_indegree_n, 
acesso_outdegree_n,
acesso_totaldegree_n,
acesso_strength,
acesso_centr_degree,
acesso_a.nn.deg,
acesso_a.nn.deg_w) %>% round(3)

#Adding type
acesso_df_degree <-cbind(acesso_df_degree, V(acesso)$LABEL_COR)

#Adding names
names(acesso_df_degree) <- c("In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree","Type")

#Ordering Variables
acesso_df_degree<-acesso_df_degree[c("Type","In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree")]

12.3 General tabel - DT

datatable(acesso_df_degree, filter = 'top')

12.4 Aggregating data from previous table - mean

aggdata_mean <-aggregate(acesso_df_degree, by=list(acesso_df_degree$Type), FUN=mean, na.rm=TRUE)

#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]
names(aggdata_mean) <- c("Group", "In Degree(M)", "Out Degree(M)", "Total Degree(M)","In Degree Normalized(M)", "Out Degree Normalized(M)", "Total Degree Normalized(M)", "Strength(M)","Centralization Degree(M)","Average Neighbor Degree(M)","Average Weighted Neighbor Degree(M)")

12.5 Aggregating data from previous table - sd

aggdata_sd <-aggregate(acesso_df_degree, by=list(acesso_df_degree$Type), FUN=sd, na.rm=TRUE) 

#Removing Type variable
aggdata_sd<-aggdata_sd[,-c(2)]
names(aggdata_sd) <- c("Group", "In Degree(SD)", "Out Degree(SD)", "Total Degree(SD)","In Degree Normalized(SD)", "Out Degree Normalized(SD)", "Total Degree Normalized(SD)", "Strength(SD)","Centralization Degree(SD)","Average Neighbor Degree(SD)","Average Weighted Neighbor Degree(SD)")

12.6 Merging mean and standart deviation

total_table <- merge(aggdata_mean,aggdata_sd,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(2) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Degree(M)","In Degree(SD)", "Out Degree(M)", "Out Degree(SD)","Total Degree(M)", "Total Degree(SD)", "In Degree Normalized(M)", "In Degree Normalized(SD)", "Out Degree Normalized(M)", "Out Degree Normalized(SD)", "Total Degree Normalized(M)", "Total Degree Normalized(SD)", "Strength(M)","Strength(SD)", "Centralization Degree(M)","Centralization Degree(SD)","Average Neighbor Degree(M)","Average Neighbor Degree(SD)","Average Weighted Neighbor Degree(M)", "Average Weighted Neighbor Degree(SD)")]

12.7 Plotting final table with round

datatable(total_table, filter = 'top')

12.8 Creating a dataframe of measures (Natureza Governamental)

acesso_df_degree <- data.frame(acesso_indegree,
acesso_outdegree, 
acesso_totaldegree,
acesso_indegree_n, 
acesso_outdegree_n,
acesso_totaldegree_n,
acesso_strength,
acesso_centr_degree,
acesso_a.nn.deg,
acesso_a.nn.deg_w) %>% round(3)

#Adding type
acesso_df_degree <-cbind(acesso_df_degree, V(acesso)$TIPO1)

#Adding names
names(acesso_df_degree) <- c("In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree","Type")

#Ordering Variables
acesso_df_degree<-acesso_df_degree[c("Type","In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree")]

12.9 General tabel - DT

datatable(acesso_df_degree, filter = 'top')

12.10 Aggregating data from previous table - mean

aggdata_mean <-aggregate(acesso_df_degree, by=list(acesso_df_degree$Type), FUN=mean, na.rm=TRUE)

#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]
names(aggdata_mean) <- c("Group", "In Degree(M)", "Out Degree(M)", "Total Degree(M)","In Degree Normalized(M)", "Out Degree Normalized(M)", "Total Degree Normalized(M)", "Strength(M)","Centralization Degree(M)","Average Neighbor Degree(M)","Average Weighted Neighbor Degree(M)")

12.11 Aggregating data from previous table - sd

aggdata_sd <-aggregate(acesso_df_degree, by=list(acesso_df_degree$Type), FUN=sd, na.rm=TRUE) 

#Removing Type variable
aggdata_sd<-aggdata_sd[,-c(2)]
names(aggdata_sd) <- c("Group", "In Degree(SD)", "Out Degree(SD)", "Total Degree(SD)","In Degree Normalized(SD)", "Out Degree Normalized(SD)", "Total Degree Normalized(SD)", "Strength(SD)","Centralization Degree(SD)","Average Neighbor Degree(SD)","Average Weighted Neighbor Degree(SD)")

12.12 Merging mean and standart deviation

total_table <- merge(aggdata_mean,aggdata_sd,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(2) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Degree(M)","In Degree(SD)", "Out Degree(M)", "Out Degree(SD)","Total Degree(M)", "Total Degree(SD)", "In Degree Normalized(M)", "In Degree Normalized(SD)", "Out Degree Normalized(M)", "Out Degree Normalized(SD)", "Total Degree Normalized(M)", "Total Degree Normalized(SD)", "Strength(M)","Strength(SD)", "Centralization Degree(M)","Centralization Degree(SD)","Average Neighbor Degree(M)","Average Neighbor Degree(SD)","Average Weighted Neighbor Degree(M)", "Average Weighted Neighbor Degree(SD)")]

12.13 Plotting final table with round

datatable(total_table, filter = 'top')

12.14 Creating a dataframe of measures (Setores)

acesso_df_degree <- data.frame(acesso_indegree,
acesso_outdegree, 
acesso_totaldegree,
acesso_indegree_n, 
acesso_outdegree_n,
acesso_totaldegree_n,
acesso_strength,
acesso_centr_degree,
acesso_a.nn.deg,
acesso_a.nn.deg_w) %>% round(3)

#Adding type
acesso_df_degree <-cbind(acesso_df_degree, V(acesso)$TIPO2)

#Adding names
names(acesso_df_degree) <- c("In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree","Type")

#Ordering Variables
acesso_df_degree<-acesso_df_degree[c("Type","In Degree", "Out Degree", "Total Degree","In Degree Normalized", "Out Degree Normalized", "Total Degree Normalized", "Strength","Centralization Degree","Average Neighbor Degree","Average Weighted Neighbor Degree")]

12.15 General tabel - DT

datatable(acesso_df_degree, filter = 'top')

12.16 Aggregating data from previous table - mean

aggdata_mean <-aggregate(acesso_df_degree, by=list(acesso_df_degree$Type), FUN=mean, na.rm=TRUE)

#Removing Type variable
aggdata_mean<-aggdata_mean[,-c(2)]
names(aggdata_mean) <- c("Group", "In Degree(M)", "Out Degree(M)", "Total Degree(M)","In Degree Normalized(M)", "Out Degree Normalized(M)", "Total Degree Normalized(M)", "Strength(M)","Centralization Degree(M)","Average Neighbor Degree(M)","Average Weighted Neighbor Degree(M)")

12.17 Aggregating data from previous table - sd

aggdata_sd <-aggregate(acesso_df_degree, by=list(acesso_df_degree$Type), FUN=sd, na.rm=TRUE) 

#Removing Type variable
aggdata_sd<-aggdata_sd[,-c(2)]
names(aggdata_sd) <- c("Group", "In Degree(SD)", "Out Degree(SD)", "Total Degree(SD)","In Degree Normalized(SD)", "Out Degree Normalized(SD)", "Total Degree Normalized(SD)", "Strength(SD)","Centralization Degree(SD)","Average Neighbor Degree(SD)","Average Weighted Neighbor Degree(SD)")

12.18 Merging mean and standart deviation

total_table <- merge(aggdata_mean,aggdata_sd,by="Group")

#Rounding
Group<-total_table[,c(1)] #Keeping group
total_table<-total_table[,-c(1)] %>% round(2) #Rouding
total_table<-cbind(Group,total_table) #Binding toghter

#Organizing Variabels
total_table<-total_table[c("Group","In Degree(M)","In Degree(SD)", "Out Degree(M)", "Out Degree(SD)","Total Degree(M)", "Total Degree(SD)", "In Degree Normalized(M)", "In Degree Normalized(SD)", "Out Degree Normalized(M)", "Out Degree Normalized(SD)", "Total Degree Normalized(M)", "Total Degree Normalized(SD)", "Strength(M)","Strength(SD)", "Centralization Degree(M)","Centralization Degree(SD)","Average Neighbor Degree(M)","Average Neighbor Degree(SD)","Average Weighted Neighbor Degree(M)", "Average Weighted Neighbor Degree(SD)")]

12.19 Plotting final table with round

datatable(total_table, filter = 'top')

13 Network plotting based only on degree measures

#Set Seed
set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(acesso, es=E(acesso), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(acesso))
maxC <- rep(Inf, vcount(acesso))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(acesso, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(acesso)$weight)

#PLotting
plot(acesso, 
     layout=co,
     edge.color=V(acesso)$color[edge.start],
     edge.arrow.size=(degree(acesso)+1)/(30*mean(degree(acesso))),
     edge.width=E(acesso)$weight/(10*mean(E(acesso)$weight)),
     edge.curved = TRUE,
     vertex.size=log((degree(acesso)+2))*(0.5*mean(degree(acesso))),
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(acesso,"LABEL_COR"),
     vertex.label.cex=log(degree(acesso)+2)/mean(degree(acesso)),
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(acesso)$LABEL_COR
b<-V(acesso)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Vertex Degree Sized - 28_ACESSO", sub = "Source: from authors ", cex = .5)
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median In Degree: %.2f\n Median Out Degree: %.2f",
     median(degree(acesso, mode="in")), 
     median(degree(acesso, mode="out"))
   ))

14 Network plotting based only on degree measures

#Set Seed
set.seed(123)

#Get Variable
V(acesso)$acesso_color_degree<-V(acesso)$totaldegree %>% round(0)

#Creating brewer pallette
vertex_acesso_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(acesso)$acesso_color_degree)), "RdBu"))(
            length(unique(V(acesso)$acesso_color_degree)))

#Saving as Vertex properties 
V(acesso)$vertex_acesso_color_degree<- vertex_acesso_color_degree[as.numeric(cut(degree(acesso),breaks =length(unique(V(acesso)$acesso_color_degree))))]

set.seed(123)
#Plotting based only on degree measures 
edge.start <- ends(acesso, es=E(acesso), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(acesso))
maxC <- rep(Inf, vcount(acesso))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(acesso, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(acesso)$weight)

#PLotting
plot(acesso, 
     layout=co,
     #edge.color=V(acesso)$color[edge.start],
     edge.arrow.size=(degree(acesso)+1)/1000,
     edge.width=E(acesso)$weight/10,
     edge.curved = TRUE,
     vertex.color=V(acesso)$vertex_acesso_color_degree,
     vertex.size=log((degree(acesso)+2))*10,
     vertex.size=20,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(acesso,"LABEL_COR"),
     vertex.label.cex=log((degree(acesso)+2))/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(acesso)$acesso_color_degree
b<-V(acesso)$vertex_acesso_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Vertex Degree Sized and Red to Blue - 28_ACESSO", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median In Degree: %.2f\nMedian Out Degree: %.2f",
     median(degree(acesso, mode="in")), 
     median(degree(acesso, mode="out"))
   ))

15 Network Plotting Centralization - Degree Measures - Using Spectral Color as Distance Measure Representation

#Set Seed
set.seed(123)

#Get Variable
V(acesso)$acesso_color_degree<-V(acesso)$acesso_centr_degree

#Creating brewer pallette
vertex_acesso_color_degree<-
  colorRampPalette(brewer.pal(length(unique(
          V(acesso)$acesso_color_degree)), "Spectral"))(
            length(unique(V(acesso)$acesso_color_degree)))

#Saving as Vertex properties 
V(acesso)$vertex_acesso_color_degree<- vertex_acesso_color_degree[as.numeric(cut(V(acesso)$acesso_color_degree,breaks =length(unique(V(acesso)$acesso_color_degree))))]

#Plotting based only on degree measures 
edge.start <- ends(acesso, es=E(acesso), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(acesso))
maxC <- rep(Inf, vcount(acesso))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(acesso, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(acesso)$weight)

#PLotting
plot(acesso, 
     layout=co,
     edge.color=V(acesso)$vertex_acesso_color_degree[edge.start],
     edge.arrow.size=(degree(acesso)+1)/10000,
     edge.width=E(acesso)$weight/10,
     edge.curved = TRUE,
     vertex.color=V(acesso)$vertex_acesso_color_degree,
     vertex.size=log((V(acesso)$acesso_centr_degree+2))*10,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(acesso,"LABEL_COR"),
     vertex.label.cex=log((degree(acesso)+2))/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(acesso)$acesso_color_degree
b<-V(acesso)$vertex_acesso_color_degree
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
e<-e[order(e$a,decreasing=T),] 
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=2,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .3)

#Adding Title
  title("Network Vertex Centralization Degree Sized Spectral Colored - 28_ACESSO", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median In Degree: %.2f\nMedian Out Degree: %.2f",
     median(degree(acesso, mode="in")), 
     median(degree(acesso, mode="out"))
   ))

16 Alternative vizualization using degree in order to genarete sub-graphs - Higher than median degree network

#Set Seed
set.seed(123)

# Network elements with lower than meadian degree
higherthanmedian.network_acesso<-V(acesso)[degree(acesso)<median(degree(acesso))] 

#Deleting vertices based in intersection betewenn acesso 
high_acesso<-delete.vertices(acesso, higherthanmedian.network_acesso)

#Plotting based only on degree measures 
edge.start <- ends(high_acesso, es=E(high_acesso), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(high_acesso))
maxC <- rep(Inf, vcount(high_acesso))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(high_acesso, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(high_acesso)$weight)

#PLotting
plot(high_acesso, 
     layout=co,
     edge.color=V(high_acesso)$color[edge.start],
     edge.arrow.size=(degree(high_acesso)+1)/1000,
     edge.width=E(high_acesso)$weight/10,
     edge.curved = TRUE,
     vertex.size=log((V(high_acesso)$acesso_centr_degree+2))*10,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(high_acesso,"LABEL_COR"),
     vertex.label.cex=log((degree(high_acesso)+2))/10,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(high_acesso)$LABEL_COR
b<-V(high_acesso)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=3,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Higher Than Median Degree - 28_ACESSO", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Mean In Degree: %.2f\n Mean Out Degree: %.2f",
     mean(degree(high_acesso, mode="in")), 
     mean(degree(high_acesso, mode="out"))
   )
  )

17 Alternative vizualization using degree in order to genarete sub-graphs - Lower than median degree network

#Set Seed
set.seed(123)

# Network elements with lower than meadian degree
lowerthanmedian.network_acesso<-V(acesso)[degree(acesso)>median(degree(acesso))] 

#Deleting vertices based in intersection betewenn acesso 
small_acesso<-delete.vertices(acesso, lowerthanmedian.network_acesso)

#Plotting based only on degree measures 
edge.start <- ends(small_acesso, es=E(small_acesso), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(small_acesso))
maxC <- rep(Inf, vcount(small_acesso))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(small_acesso, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(small_acesso)$weight)

#PLotting
plot(small_acesso, 
     layout=co,
     edge.color=V(small_acesso)$color[edge.start],
     edge.arrow.size=(degree(small_acesso)+1)/1000,
     edge.width=E(small_acesso)$weight/10,
     edge.curved = TRUE,
     vertex.size=log((V(small_acesso)$acesso_centr_degree+2))*20,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(small_acesso,"LABEL_COR"),
     vertex.label.cex=log((degree(small_acesso)+2))/3,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)

#Solving Problems with legend rendering 
a<-V(small_acesso)$LABEL_COR
b<-V(small_acesso)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=4,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Smaller Than Median Degree - 28_ACESSO", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Mean In Degree: %.2f\nMean Out Degree: %.2f",
     mean(degree(small_acesso, mode="in")), 
     mean(degree(small_acesso, mode="out"))
   )
  )

18 Plotting using Average Neighbor Degree

#Set Seed
set.seed(123)

#Plotting based only on degree measures 
edge.start <- ends(acesso_simplified, es=E(acesso_simplified), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(acesso_simplified))
maxC <- rep(Inf, vcount(acesso_simplified))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(acesso_simplified, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(acesso_simplified)$weight)

#Plotting based only on degree measures  #acesso_simplified_a.nn.deg
V(acesso_simplified)$acesso_a.nn.deg<-as.numeric(graph.knn(acesso_simplified)$knn)
V(acesso_simplified)$acesso_a.nn.deg[V(acesso_simplified)$acesso_a.nn.deg=="NaN"]<-0

#PLotting
plot(acesso_simplified, 
     layout=co,
     edge.color=V(acesso_simplified)$color[edge.start],
     edge.arrow.size=sqrt((V(acesso_simplified)$acesso_a.nn.deg)^2+1)/1000,
     edge.width=E(acesso_simplified)$weight/100,
     edge.curved = TRUE,
     vertex.color=V(acesso_simplified)$color,
     vertex.size=(sqrt((V(acesso_simplified)$acesso_a.nn.deg)^2))/5,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(acesso_simplified,"LABEL_COR"),
     vertex.label.cex=(sqrt((V(acesso_simplified)$acesso_a.nn.deg)^2)+1)/500,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(acesso_simplified)$LABEL_COR
b<-V(acesso_simplified)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=4,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Average Neighbor Degree Sized - 28_ACESSO", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median Average Neighbor Degree: %.2f",
     median((acesso_a.nn.deg+1))
   ))

19 Plotting using Average Neighbor Degree

#Set Seed
set.seed(123)

#Plotting based only on degree measures 
edge.start <- ends(acesso_simplified, es=E(acesso_simplified), names=F)[,1]

# Fixing ego
minC <- rep(-Inf, vcount(acesso_simplified))
maxC <- rep(Inf, vcount(acesso_simplified))
minC[1] <- maxC[1] <- 0
co <- layout_with_fr(acesso_simplified, niter=10000, minx=minC, maxx=maxC,miny=minC, maxy=maxC, weights = E(acesso_simplified)$weight)


#Plotting based only on degree measures  #acesso_a.nn.deg
V(acesso_simplified)$acesso_a.nn.deg_w<-as.numeric(graph.knn(acesso_simplified, weights = E(acesso_simplified)$weight)$knn)
V(acesso_simplified)$acesso_a.nn.deg_w[V(acesso_simplified)$acesso_a.nn.deg_w=="NaN"]<-0

#PLotting
plot(acesso_simplified, 
     layout=co,
     edge.color=V(acesso_simplified)$color[edge.start],
     edge.arrow.size=sqrt((V(acesso_simplified)$acesso_a.nn.deg_w)^2+1)/1000,
     edge.width=E(acesso_simplified)$weight/100,
     edge.curved = TRUE,
     vertex.color=V(acesso_simplified)$color,
     vertex.size=(sqrt((V(acesso_simplified)$acesso_a.nn.deg_w)^2))/5,
     vertex.frame.color="#ffffff",
     vertex.label.color="black",
     vertex.label=get.vertex.attribute(acesso_simplified,"LABEL_COR"),
     vertex.label.cex=(sqrt((V(acesso_simplified)$acesso_a.nn.deg_w)^2)+1)/500,
     vertex.label.dist=0,
     rescale=F,
     xlim=range(co[,1]), 
     ylim=range(co[,2]))
axis(1)
axis(2)


#Solving Problems with legend rendering 
a<-V(acesso_simplified)$LABEL_COR
b<-V(acesso_simplified)$color
c<-table(a,b)
d<-as.data.frame(c)
e<-subset(d, d$Freq>0)
f<-t(e$a)
g<-t(e$b)

#Adding Legend
legend(x=range(co[,1])[2], y=range(co[,2])[2],
       legend=as.character(f),
       pch=21,
       col = "#777777", 
       pt.bg=as.character(g),
       pt.cex=4,
       bty="n", 
       ncol=1,
       lty=1,
       cex = .5)

#Adding Title
  title("Network Average Weighted Neighbor Degree Sized - 28_ACESSO", sub = "Source: from authors ")
  text(x=range(co[,1])[1], y=range(co[,2])[1], labels = 
   sprintf("Median Average Weighted Neighbor Degree: %.2f",
     median((acesso_a.nn.deg_w+1))
   ))

20 Circle Degree Too intensive computation #code

#Circle Degree ***Too intense computation***
#A_acesso <- get.adjacency(acesso, sparse=FALSE)
#detach("package:igraph", unload=TRUE)
#library(network)
#g <- network::as.network.matrix(A_acesso)
#library(sna)
#gplot.target(g, degree(g), main="Circle Degree")
#library(igraph)

21 Saving objects with new variables and changes

save.image("~/SNArRDJF/Robject/acesso_data.RData")